package cn.hls.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import cn.hls.dao.UserDao;
import cn.hls.entity.User;
import cn.hls.entity.vo.PageInfo;

/**
 * @fileName: GenericsUtils.java
 * @descrption: 用户信息Dao类
 * @author : yinghui.zhang
 * @Email: uuhui@163.com
 * @date 2015-4-7
 * @version 1.0
 */
@Component("userDaoImpl")
public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao {

	@Override
	public void deleteUser(Long userid) throws Exception {
		// 清除用户所在的角色
		clearUserRole(userid);
		// 清除用户所拥有的权限信息
		// this.extAclService.delPermission("User", userid);
		// 删除用户信息
		delete(userid);
	}

	/**
	 * 清除用户的角色信息
	 * 
	 * @param userid
	 *            用户编号
	 */
	private void clearUserRole(Long userId) throws Exception {
		String hql = "delete from UsersRoles ur where ur.user.id = ? ";
		getSession().createQuery(hql).setParameter(0, userId).executeUpdate();
	}

	@Override
	public PageInfo<User> loadUsers(PageInfo<User> page, User user) throws Exception {
		// 定义查询hql语句
		StringBuilder selectStringBuilder = new StringBuilder();
		// 定义汇总查询hql语句
		StringBuilder counthql = new StringBuilder();
		// 定义查询参数
		List<Object> paramList = new ArrayList<Object>();
		selectStringBuilder.append("from User u where  deleted=?  ");
		paramList.add(false);
		if (user != null) {
			// 按登录名称查询
			if (StringUtils.hasText(user.getLoginName())) {
				selectStringBuilder.append(" and p.userName like ?");
				paramList.add(user.getLoginName());
			}
		}
		counthql.append(" select count(id) ").append(selectStringBuilder);
		return find(selectStringBuilder.toString(), counthql.toString(), paramList.size() == 0 ? null : paramList.toArray(), page);
	}

	@Override
	public User login(String loginName) throws Exception {
		return (User) getSession().createQuery("select u from User u where u.loginName = ? and u.deleted=false ").setParameter(0, loginName).uniqueResult();
	}

	@Override
	public boolean checkUserName(String loginName) {
		String hql = "select count(id) from User where username=? and deleted=false";
		List<User> list = findByHql(hql, new Object[] { loginName });
		if ((list != null) && (list.get(0) != null) && (Integer.parseInt(list.get(0).toString()) > 0)) {
			return true;
		}
		return false;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List searchUsersOfRole(String roleName)throws Exception {
		String hql = "select u.username from UsersRoles ur join ur.user u join ur.role r where r.name = ? and r.deleted=false and u.deleted=false";
		//return getHibernateTemplate().find(hql, roleName);
		return (List) find(hql,new Object[]{roleName});
	}

	@Override
	public boolean updatePwd(User entity) throws Exception {
		String hql="update User set password=?,salt=?  where id=?";
		int result=executeHQLUpdate(hql, new Object[]{entity.getPassword(),entity.getSalt(),entity.getId()});
		return result>0?true:false;
	}
}
